﻿using System;
using System.Drawing;
using System.Drawing.Printing;
using System.IO;
using System.Windows.Forms;

namespace Parabola
{
    class BluePrinting
    {
        private static int pagenum;
        private static Bitmap bmp;
        private static string titletext;

        public static bool PrintBmp(Bitmap bitmap, string title)
        {
            bmp = bitmap;
            titletext = title;
            PrinterSettings printsettings = null;
            if (PrintDialog(ref printsettings))
            {
                PrintDocument printdoc = new PrintDocument();
                printdoc.PrinterSettings = printsettings;
                printdoc.DocumentName = Path.GetFileName(title);
                printdoc.DefaultPageSettings.Margins = new Margins(0, 0, 0, 0);
                printdoc.OriginAtMargins = true;
                pagenum = 0;
                printdoc.PrintPage += print_page;
                printdoc.Print();
                return true;
            }
            else
            {
                return false;
            }
        }

        private static void print_page(object sender, PrintPageEventArgs e)
        {
            //Dim characters, lines As Integer
            //Dim printfont As New Font("Lucida Console", 9)
            Font titlefont = new Font("Arial Bold", 9);
            //Dim printformat As New StringFormat(StringFormatFlags.LineLimit)
            StringFormat titleformat = new StringFormat();
            titleformat.Alignment = StringAlignment.Far;
            Size picsize = new Size(bmp.Size.Width, bmp.Size.Height);
            Size printsize = new Size(e.PageBounds.Width - 140, e.PageBounds.Height - 140);
            if (bmp.Width > printsize.Width)
            {
                picsize.Width = printsize.Width;
                double wfactor = (double)picsize.Width / (double)bmp.Width;
                picsize.Height = (int)(wfactor * bmp.Height);
            }
            if (bmp.Height > printsize.Height)
            {
                picsize.Height = printsize.Height;
                double hfactor = (double)picsize.Height / (double)bmp.Height;
                picsize.Width = (int)(hfactor * bmp.Width);
            } 
            Rectangle printarea = new Rectangle(90, 70, printsize.Width, printsize.Height);
            //Dim printtext As String = richbox.Text.Substring(total)
            //Dim size As SizeF = e.Graphics.MeasureString(printtext, printfont, printsize, printformat, characters, lines)
            pagenum += 1;
            //total += characters;
            e.Graphics.DrawImage(bmp, new Rectangle(printarea.X, printarea.Y, picsize.Width, picsize.Height));
            e.Graphics.DrawString(titletext, titlefont, Brushes.Black, printsize.Width + 90, 30, titleformat);
            //e.Graphics.DrawString(printtext, printfont, Brushes.Black, printarea, printformat)
            string footer = "Page " + pagenum.ToString();
            e.Graphics.DrawString(footer, titlefont, Brushes.Black, printsize.Width + 90, e.PageBounds.Height - 65, titleformat);
            /*If total< MainForm.txtMain.Text.Length Then
                e.HasMorePages = True
            Else
                e.HasMorePages = False
            End If*/
        }

        private static bool PrintDialog(ref PrinterSettings settings)
        {
            PrintDialog pfd = new PrintDialog();
            if (pfd.ShowDialog() == DialogResult.OK)
            {
                settings = pfd.PrinterSettings;
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
